<?php
defined('SYSPATH') or die('No direct script access.') ;

/**
 * 生成商品sitemap
 * @version $Id: sitemap.php 57 2011-12-30 10:20:27Z 闵新青 $
 */
class Cli_Sitemap extends CLI
{

    protected  $xml_str = '';

    public function  __construct(CLI_Args $args = NULL) {
        parent::__construct($args);

        $this->xml_str .= '<?xml version="1.0" encoding="UTF-8" ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
        $this->xml_str .= '<url><loc>http://www.360hqb.com/</loc><priority>1.00</priority><lastmod>'.date('Y-m-d').'</lastmod><changefreq>daily</changefreq></url>';
    }

    public function  __destruct() {
        $this->xml_str .= '</urlset>';
        file_put_contents (DOCROOT . '/sitemap.xml', $this->xml_str);;
    }

    public function main()
	{
        
        try{
            // 商品
            $shops = array();
            $select = DB::select('shop_id', 'cert_type')
                ->from('shops')
                ->where('deleted', '=', 0)
                ->where('status', '=', 4)
                ->where('cert_type', '>', 0)
                ->fetch_all();
            foreach ($select as $row)
            {
                $shops[] = $row['shop_id'];
            }

            $on_sale_goods = DB::select('goods_id', 'shop_id')
                ->from('goods')
                ->where('deleted', '=', 0)
                ->where('on_sale', '=', 1)
                ->where('expire_time', '>', time())
                ->fetch_all();

            foreach ($on_sale_goods as $k => $item)
            {
                if (in_array($item['shop_id'], $shops))
                {
                    $this->xml_str .= $this->get_xml($item['goods_id'], 'item');
                }
                unset($on_sale_goods[$k]);
            }
            unset($shops);

            //SPU
            $products = DB::select('product_id')
                ->from('products')
                ->where('status', '=', 3)
                ->fetch_all();

            foreach ($products as $k => $p)
            {
                try{
                    // java接口获取商品数据
                    $response = new Spu_Search($p['product_id']);
                    $spu_search = $response->get_response();
                    if ($spu_search)
                    {
                        $item_ids = $spu_search->get_item_ids();
                        if ($item_ids)
                        {
                            $item_ids = explode(',', $item_ids);
                            foreach ($item_ids as $k => $v)
                            {
                                $this->xml_str .= $this->get_xml($p['product_id'].'-'.($k+1), 'spu');
                            }
                        }
                    }

                    $response = new Spu_Search('0'.$p['product_id']);
                    $spu_search = $response->get_response();
                    if ($spu_search)
                    {
                        $item_ids = $spu_search->get_item_ids();
                        if ($item_ids)
                        {
                            $item_ids = explode(',', $item_ids);
                            foreach ($item_ids as $k => $v)
                            {
                                $this->xml_str .= $this->get_xml('0'.$p['product_id'].'-'.($k+1), 'spu');
                            }
                        }
                    }
                }
                catch (Exception $e)
                {
                    sleep(5);
                    echo $e->getMessage();
                }
            }

            $spu_search = new Goods_Spusearch();
            $cat_tree = $spu_search->get_cat_map_tree();
            foreach($cat_tree as $k => $cat)
            {
                if ($cat['is_parent'] == 0)
                {
                    $this->xml_str .= $this->get_xml($cat['cat_map_id'], 'spusearch');
                }
            }

        }
        catch (Exception $e)
        {
            echo $e->getMessage();
        }
	}

    protected function get_xml($id, $type)
    {
        $str = '<url><loc>';
        if ($type == 'item')
        {
            $str .= 'http://item.360hqb.com/'.$id.'.html';
        }
        elseif ($type == 'spu')
        {
            $str .= 'http://item.360hqb.com/spu-'.$id.'.html';
        }
        elseif ($type == 'spusearch')
        {
            $str .= 'http://www.360hqb.com/spusearch?c='.$id;
        }
        $str .= '</loc><priority>1.00</priority><lastmod>'.date('Y-m-d').'</lastmod><changefreq>daily</changefreq></url>';
        return $str;
    }

}